# Definition for singly-linked list.
from typing import Optional


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
        if not headA or not headB:
            return None
        cur_a = headA
        cur_b = headB
        while cur_b != cur_a:
            cur_a = cur_a.next if cur_a else headB
            cur_b = cur_b.next if cur_b else headA
            # if cur_a is None and cur_b is None:
            #     return None
            # 这里不需要考虑a和b同时为None的情况,因为None==None
        return cur_a

